function idCardChange(){
	var idCard = $(":input[name='idCardNo']").val();
	if(idCard.length != 18) return;
	$("#birthday").val(getBirthday(idCard));
}

function getBirthday(psidno){
    var birthdayno,birthdaytemp
    if(psidno.length==18){
        birthdayno=psidno.substring(6,14)
    }else if(psidno.length==15){
        birthdaytemp=psidno.substring(6,12)
        birthdayno="19"+birthdaytemp
    }else{
        alert("错误的身份证号码，请核对！")
        return false
    }
    var birthday=birthdayno.substring(0,4)+"-"+birthdayno.substring(4,6)+"-"+birthdayno.substring(6,8)
    return birthday    
}


$(function(){
	
	$("#submit").click(function(){
		if($("#edit").valid()){
			if($(":input[name='source']").val() == "-1"){
				swal("请选择来源", "", "error");
				return;
			}
			
			if($(":input[name='sex']").val() == "-1"){
				swal("请选择性别", "", "error");
				return;
			}
			
			if($(":input[name='levelId']").val() == "-1"){
				swal("请选择权益", "", "error");
				return;
			}
			
			var param = $("#edit").serialize() + "";
			param = param.replace(/=-1/g, "=");
			$.post(contextpath + "customer/save", param, function(){
				swal({
					title : "编辑渠道商成功",
					type : "success",
					confirmButtonText : "确定",
					closeOnConfirm : false
				},function() {
					window.location.href = contextpath + "/customer?view=1&type=1&auditStatus=1";
				});
			})
		}
	});
	
	initDateByNameAndFormat('cooperationStartTime', 'YYYY-MM-DD');
	$(':input[name="cooperationStartTime"]').val(formatDate2($("#cooperationStartTimeHidden").val()));
	
	initDateByNameAndFormat('cooperationEndTime', 'YYYY-MM-DD');
	$(':input[name="cooperationEndTime"]').val(formatDate2($("#cooperationEndTimeHidden").val()));
	
	
	$('select').select2({
	    minimumResultsForSearch: Infinity
	});	
	
	$('#recommendDistributorId').select2({
	    minimumResultsForSearch: Infinity,
        placeholder:"请选择或输入渠道商",  //提示语
        maximumSelectionLength :1,   //限制搜索的个数
        tags: false                //可以手动添加，若限制手动添加，设置为false
	});	
});

function formatDate2 (strTime) {
	if(!strTime){
		return '';
	}
    var date = new Date(strTime);
    return date.Format("yyyy-MM-dd"); 
}

function formatDate (strTime) {
	if(!strTime){
		return '';
	}
    var date = new Date(strTime);
    return date.Format("yyyy-MM-dd hh:mm"); 
}